package com.fenghuajueli.module_find_the_difference.zhao;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.module_find_the_difference.R;
import com.fenghuajueli.module_find_the_difference.SPHelper;
import com.fenghuajueli.module_find_the_difference.dialog.AnswerCorrectDialog;
import com.fenghuajueli.module_find_the_difference.dialog.AnswerErrorDialog;
import com.fenghuajueli.module_find_the_difference.entity.Zhaocha;
import com.fenghuajueli.module_find_the_difference.view.ZhaoView;
import com.fenghuajueli.module_find_the_difference.widget.CustomHorizontalProgresNoNum;
import com.fenghuajueli.module_route.UserModuleRoute;

import java.util.ArrayList;
import java.util.List;

public class ZhaoActivity extends BaseActivity implements ZhaoActivityContract.View {
    private ImageView ivXin2;
    private ImageView ivXin1;
    private ImageView ivXin3;

    private ZhaoView ivTu1;
    private ZhaoView ivTu2;
    private FrameLayout layoutAd;
    private int curIndex, index, totalSize, cuoCount, progress;
    private TextView tvProgress;
    private CustomHorizontalProgresNoNum horizontalProgress;
    private ZhaoActivityContract.Presenter presenter;
    private List<Zhaocha> list = new ArrayList<>();

    public static void start(@NonNull Context context, int guan) {
        Intent intent = new Intent(context, ZhaoActivity.class);
        intent.putExtra("guan", guan);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhaocha);
        ivXin1 = findViewById(R.id.iv_xin_1);
        ivXin2 = findViewById(R.id.iv_xin_2);
        ivXin3 = findViewById(R.id.iv_xin_3);
        tvProgress = findViewById(R.id.tv_progress);
        horizontalProgress = findViewById(R.id.horizontalProgress);
        ivTu1 = findViewById(R.id.iv_tu_1);
        ivTu2 = findViewById(R.id.iv_tu_2);
        layoutAd = findViewById(R.id.layout_ad);
        index = SPHelper.getInstance(this).getIndex();
        findViewById(R.id.iv_back).setOnClickListener(v -> onBackPressed());
        presenter = new ZhaoActivityPresenter(this);
        ivTu1.setCallback(new ZhaoView.Callback() {
            @Override
            public void onFalse() {
                cuoCount++;
                showErrorMsg();
                showCuoCount();
            }

            @Override
            public void onTrue(int x, int y) {
                ivTu2.addPoint(x, y);
                horizontalProgress.setProgress(++progress);
                tvProgress.setText(progress + "/" + totalSize);
            }

            @Override
            public void onEnd() {
                AnswerCorrectDialog answerCorrectDialog = new AnswerCorrectDialog(ZhaoActivity.this, new AnswerCorrectDialog.OnAnswerCorrectListener() {
                    @Override
                    public void back() {
                        finish();
                    }

                    @Override
                    public void next() {
                        curIndex++;
                        cuoCount = 0;
                        progress = 0;
                        horizontalProgress.setProgress(0);
                        doGame();
                    }
                });
                answerCorrectDialog.setCancelable(false);
                answerCorrectDialog.show();
            }
        });

        ivTu2.setCallback(new ZhaoView.Callback() {
            @Override
            public void onFalse() {
                cuoCount++;
                showErrorMsg();
                showCuoCount();
            }

            @Override
            public void onTrue(int x, int y) {
                ivTu1.addPoint(x, y);
                horizontalProgress.setProgress(++progress);
                tvProgress.setText(progress + "/" + totalSize);
            }

            @Override
            public void onEnd() {

                AnswerCorrectDialog answerCorrectDialog = new AnswerCorrectDialog(ZhaoActivity.this, new AnswerCorrectDialog.OnAnswerCorrectListener() {
                    @Override
                    public void back() {
                        finish();
                    }

                    @Override
                    public void next() {
                        curIndex++;
                        cuoCount = 0;
                        progress = 0;
                        horizontalProgress.setProgress(0);
                        doGame();
                    }
                });
                answerCorrectDialog.setCancelable(false);
                answerCorrectDialog.show();
            }
        });

        curIndex = getIntent().getIntExtra("guan", 0);
        presenter.takeView(this, getIntent().getExtras());
    }

    private void showErrorMsg() {
        if (cuoCount == 1) {
            ToastUtils.showShort("找错啦，还有两次机会~");
        } else if (cuoCount == 2) {
            ToastUtils.showShort("找错啦，只剩最后一次机会了~");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (curIndex > index) {
            index = curIndex;
            SPHelper.getInstance(this).setIndex(index - 1);
        }
        presenter.dropView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.resumeView();
    }

    @Override
    protected void onPause() {
        presenter.pauseView();
        super.onPause();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        presenter.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void showData(List<Zhaocha> zhaochas, int guanKa) {
        list.clear();
        list.addAll(zhaochas);
        int width = ivTu1.getMeasuredWidth();
        int height = ivTu1.getMeasuredHeight();
        for (int i = guanKa; i < list.size(); i++) {
            Zhaocha zhaocha = list.get(i);
            for (int j = 0; j < zhaocha.list.size(); j++) {
                int x = zhaocha.list.get(j).x;
                int y = zhaocha.list.get(j).y;
                int w = zhaocha.list.get(j).w;
                int h = zhaocha.list.get(j).h;
                zhaocha.list.get(j).x = (int) ((float) x / 600 * width);
                zhaocha.list.get(j).y = (int) ((float) y / 500 * height);
                zhaocha.list.get(j).w = (int) ((float) w / 600 * width);
                zhaocha.list.get(j).h = (int) ((float) h / 500 * height);
            }
        }
        cuoCount = 0;
        doGame();
    }

    @Override
    public void showTishiCount(int c) {
    }

    @Override
    public void showTishi() {
        ivTu1.showTip();
    }


    private void doGame() {
        if (curIndex >= 0 && curIndex < list.size()) {
            showZhaoView();
        } else {
            ToastUtils.showShort("已经是最后一关了！");
        }
    }


    int[] list1 = new int[]{R.mipmap.a_19, R.mipmap.a_07, R.mipmap.a_22, R.mipmap.a_01, R.mipmap.a_02, R.mipmap.a_03
            , R.mipmap.a_04, R.mipmap.a_05, R.mipmap.a_06, R.mipmap.a_14,
            R.mipmap.a_08, R.mipmap.a_09, R.mipmap.a_10, R.mipmap.a_11, R.mipmap.a_12, R.mipmap.a_13, R.mipmap.a_15, R.mipmap.a_25,
            R.mipmap.a_17, R.mipmap.a_18, R.mipmap.a_20, R.mipmap.a_21, R.mipmap.a_26, R.mipmap.a_23, R.mipmap.a_24, R.mipmap.a_16};
    int[] list2 = new int[]{R.mipmap.a_19_d, R.mipmap.a_07_d, R.mipmap.a_22_d, R.mipmap.a_01_d, R.mipmap.a_02_d, R.mipmap.a_03_d,
            R.mipmap.a_04_d, R.mipmap.a_05_d, R.mipmap.a_06_d, R.mipmap.a_14_d,
            R.mipmap.a_08_d, R.mipmap.a_09_d, R.mipmap.a_10_d, R.mipmap.a_11_d, R.mipmap.a_12_d, R.mipmap.a_13_d, R.mipmap.a_15_d, R.mipmap.a_25_d,
            R.mipmap.a_17_d, R.mipmap.a_18_d, R.mipmap.a_20_d, R.mipmap.a_21_d, R.mipmap.a_26_d, R.mipmap.a_23_d, R.mipmap.a_24_d, R.mipmap.a_16_d};

    private void showZhaoView() {
        Zhaocha zhaocha = list.get(curIndex);
        RoundedCorners roundedCorners = new RoundedCorners(12);//数字为圆角度数
        RequestOptions coverRequestOptions = new RequestOptions()
                .transforms(new CenterCrop(), roundedCorners)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                .skipMemoryCache(true);//不做内存缓存

        Glide.with(ZhaoActivity.this).load(list1[curIndex]).apply(coverRequestOptions).into(ivTu1);
        Glide.with(ZhaoActivity.this).load(list2[curIndex]).apply(coverRequestOptions).into(ivTu2);
        ivTu1.setZhaocha(zhaocha.list);
        ivTu2.setZhaocha(zhaocha.list);
        totalSize = zhaocha.list.size();
        horizontalProgress.setMax(totalSize);
        tvProgress.setText("0/" + totalSize);
        showCuoCount();
    }

    private void showCuoCount() {

        if (cuoCount == 0) {
            ivXin1.setImageResource(R.mipmap.aa_dianliang);
            ivXin2.setImageResource(R.mipmap.aa_dianliang);
            ivXin3.setImageResource(R.mipmap.aa_dianliang);
        } else if (cuoCount == 1) {
            ivXin1.setImageResource(R.mipmap.aa_weidianliang);
            ivXin2.setImageResource(R.mipmap.aa_dianliang);
            ivXin3.setImageResource(R.mipmap.aa_dianliang);
        } else if (cuoCount == 2) {
            ivXin1.setImageResource(R.mipmap.aa_weidianliang);
            ivXin2.setImageResource(R.mipmap.aa_weidianliang);
            ivXin3.setImageResource(R.mipmap.aa_dianliang);
        } else if (cuoCount == 3) {
            ivXin1.setImageResource(R.mipmap.aa_weidianliang);
            ivXin2.setImageResource(R.mipmap.aa_weidianliang);
            ivXin3.setImageResource(R.mipmap.aa_weidianliang);

            AnswerErrorDialog answerErrorDialog = new AnswerErrorDialog(ZhaoActivity.this, new AnswerErrorDialog.OnAnswerErrorListener() {
                @Override
                public void replay() {
                    cuoCount = 0;
                    progress = 0;
                    horizontalProgress.setProgress(0);
                    tvProgress.setText("0/" + totalSize);
                    doGame();
                }

                @Override
                public void back() {
                    finish();
                }
            });
            answerErrorDialog.setCancelable(false);
            answerErrorDialog.show();
        }
    }
}
